﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using ICSharpCode.NRefactory.Completion;
using ICSharpCode.NRefactory.Editor;

namespace Peter.Logic.AvalonEdit
{
   /// <summary>
   /// Represents some sort of completion data.
   /// </summary>
   class CompletionData : ICompletionData, ICSharpCode.AvalonEdit.CodeCompletion.ICompletionData
   {
      private double m_Priority = 1;
      private readonly List<ICompletionData> m_OverloadedData = new List<ICompletionData> ();

      protected CompletionData () { }

      /// <summary>
      /// Initializes a new completion data object.
      /// </summary>
      /// <param name="text">Text of completion object.</param>
      public CompletionData (string text)
      {
         DisplayText = CompletionText = Description = text;
      }

      public string TriggerWord { get; set; }
      public int TriggerWordLength { get; set; }

      #region NRefactory ICompletionData implementation
      public CompletionCategory CompletionCategory { get; set; }
      public string DisplayText { get; set; }
      public virtual string Description { get; set; }
      public string CompletionText { get; set; }
      public DisplayFlags DisplayFlags { get; set; }

      /// <summary>
      /// Gets if the completion data has overloads.
      /// </summary>
      public bool HasOverloads
      {
         get { return this.m_OverloadedData.Count > 0; }
      }

      /// <summary>
      /// Gets overload completion data.
      /// </summary>
      public IEnumerable<ICompletionData> OverloadedData
      {
         get { return this.m_OverloadedData; }
      }

      /// <summary>
      /// Adds an overload completion data.
      /// </summary>
      /// <param name="data">Overload completion data to add.</param>
      public void AddOverload (ICompletionData data)
      {
         if (this.m_OverloadedData.Count == 0)
            this.m_OverloadedData.Add (this);
         this.m_OverloadedData.Add (data);
      }
      #endregion

      #region AvalonEdit ICompletionData implementation

      public ImageSource Image { get; set; }

      /* Convert Text Block (ImageDisplay) to image...
        Canvas c1 = new Canvas();
        TextBlock t = new TextBlock();
        t.Text = text1.Text;
        t.FontFamily = text1.FontFamily;
        t.Foreground = text1.Foreground;
        t.FontSize = text1.FontSize;
        c1.Children.Add(t);
        WriteableBitmap wbmp = new WriteableBitmap(c1, null);
        im = new Image();
        im.Source = wbmp;
        im.Height = 200;
        im.Width = 200; */

      public virtual void Complete (ICSharpCode.AvalonEdit.Editing.TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
      {
         textArea.Document.Replace (completionSegment, this.CompletionText);
      }

      public object Content
      {
         get { return this.DisplayText; }
      }

      object ICSharpCode.AvalonEdit.CodeCompletion.ICompletionData.Description
      {
         get { return this.Description; }
      }

      public double Priority
      {
         get { return this.m_Priority; }
         set { this.m_Priority = value; }
      }

      public string Text
      {
         get { return this.CompletionText; }
      }
      #endregion

      #region Equals, ToString, GetHashCode...
      public override string ToString ()
      {
         return DisplayText;
      }

      public override bool Equals (object obj)
      {
         var other = obj as CompletionData;
         return other != null && DisplayText == other.DisplayText;
      }

      public override int GetHashCode ()
      {
         return DisplayText.GetHashCode ();
      }
      #endregion
   }
}
